<?php

namespace App\Admin\Controllers;

use App\Http\Controllers\Controller;
use App\Models\Base\LogAccountBand;
use App\Models\Login\Account;
use App\Service\RequestService;
use Encore\Admin\Controllers\ModelForm;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Illuminate\Support\Facades\Input;

/**
 * 账号禁封日志.
 */
class AccountLogController extends Controller
{
    use ModelForm;

    public function index()
    {
        return Admin::content(function (Content $content) {
            $content->header(trans('game.player_related'));
            $content->description(trans('game.account_banned_log'));
            $content->body($this->grid()->render());
        });
    }

    protected function grid()
    {
        $grid = Admin::grid(LogAccountBand::class, function (Grid $grid) {
            $grid->model()->orderBy('id', 'desc');
            $grid->id('ID');
            $grid->account(trans('game.account'));
            $grid->columns([
                'account_id' => trans('game.account_id'),
                'platform_id' => 'game.platform_id',
            ]);
            $grid->platformName()->name(trans('game.channel'));
            $grid->status(trans('game.status'))->display(function ($status) {
                return $status ? "<span class='label bg-green'>".trans('game.normal')."</span>" : "<span class='label bg-red'>".trans('game.lock')."</span>";
            });
            $grid->columns([
                'created_at' => trans('game.created_at'),
                'updated_at' => trans('game.updated_at'),
            ]);
            $grid->disableRowSelector();
            $grid->disableCreateButton();
            $grid->actions(function ($actions) use ($grid) {
                $actions->disableDelete();
            });
        });

        return $grid;
    }

    protected function form()
    {
        return Admin::form(LogAccountBand::class, function (Form $form) {
            $form->radio('status', trans('game.status'))->options(['1' => trans('game.normal')])->rules('required');
            $form->saving(function (Form $form) {
                $status = Input::post('status');
                if (null == $status) {
                    return back()->with(admin_toastr(trans('game.select_state'), 'error'))->withInput();
                }
                if ('PUT' == request()->method()) {
                    $ret = RequestService::banAccount($form->model()->platform_id, $form->model()->account, 0);
                    if (!$ret) {
                        return back()->with(admin_toastr(trans('game.gs_communication_failed'), 'error'))->withInput();
                    }
                }
            });
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     *
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {
            $content->header(trans('game.player_related'));
            $content->description(trans('game.update_state'));
            $content->body($this->form()->edit($id));
        });
    }

    public function update($id)
    {
        $log = LogAccountBand::find($id);
        $info = Account::find($log->account_id);
        $ret = RequestService::banAccount($info->ac_platform, $info->ac_account, 1);
        if (!$ret) {
            return back()->with(admin_toastr(trans('game.gs_communication_failed'), 'error'))->withInput();
        }

        return $this->form()->update($id);
    }
}
